import React from 'react';
import {  Button, Input, Form, Switch, Modal,message,InputNumber } from 'antd';
import _ from 'lodash';
const FormItem = Form.Item;
import {fetchArgs,FetchURL,createData,updateData,changePage,_validate,check_rules} from '../store/actions';

class FormCheckType extends React.Component{
    constructor(props){
        super(props);
        this.state={
            visible:this.props.visible || false,
            title:this.props.title || '新增检查分类',
            targetData:this.props.targetData || {}
        };
        this.onOk = this.onOk.bind(this);
        this.onCancel = this.onCancel.bind(this);
        this.resetFields = this.resetFields.bind(this);
    };
    static contextTypes = {
        router : React.PropTypes.object
    };
    resetFields(){
        this.props.form.resetFields();
    };
    onOk(e){
        const _this = this
        _validate(e,_this,function (data) {
            data.code = data.code == null ? 0 : data.code;
            data.status = (typeof data.status === 'boolean') ? (data.status ? 1 : 0) : (data.status === null ? 0 : data.status);
            let sendData = Object.assign({}, data, {});
            let _args = Object.assign({},fetchArgs);
            _args.URL = FetchURL.CHECKTYPE;
            _args.IsAuth = true;

            if(!_this.props.id) {
                _args.SendData = sendData;
                _args.Succ = function (res) {
                    _this.onCancel();
                    message.success('新增检查分类成功', 1)
                    _this.setState({
                        visible:false,
                        id:null,
                        title:'',
                        targetData:null
                    },function () {
                        _this.props.reFetch()
                    });
                };
                _args.Fail = function () {
                    message.error('新增检查分类失败', 1);
                }
                _this.props.dispatch(createData(_args));
            }
            else {
                sendData = Object.assign({}, data, {id : parseInt(_this.props.id)});
                _args.SendData = sendData;
                _args.Succ = function (res) {
                    _this.onCancel();
                    message.success('修改检查分类成功', 1)
                    _this.setState({
                        visible:false,
                        id:null,
                        title:'',
                        targetData:null
                    },function () {
                        _this.props.reFetch()
                    });
                };
                _args.Fail = function () {
                    message.error('修改检查分类失败', 1);
                }
                _this.props.dispatch(updateData(_args));
            }
        });
    };
    onCancel(){
        this.props.onCancel();
        this.resetFields();
    };
    setInitialValue(data){
        const  {setFieldsValue} = this.props.form;
        setFieldsValue(data);
    };
    componentWillMount(){
        const _this = this;
        this.props.dispatch(check_rules(function(rules){
            _this.setState({
                rules:rules.content
            })
        }));
        const {setFieldsValue} = this.props.form;
    };
    componentWillReceiveProps(nextProp){
        this.setState({
            visible:nextProp.visible,
            title:nextProp.title,
            targetData:nextProp.targetData
        });
    };
    componentDidMount(){
        this.setInitialValue(this.state.targetData);
    };
    render(){
        const {getFieldProps} = this.props.form;
        const formItemLayout = {
            labelCol: { span: 6 },
            wrapperCol: { span: 16 }
        };
        const name = getFieldProps('name',{
            rules:[
                {required:true,message:'请输入名称'}
            ],
            initialValue:this.state.targetData && this.state.targetData.name
        });
        const code = getFieldProps('code',{
            rules:[
                { required : false,type:'number' , message : '请输入编码' }
            ],
            initialValue: this.state.targetData && this.state.targetData.code
        });
        const mnemonic = getFieldProps('mnemonic',{
            rules:[
                {required : false , message : '请输入助记符'}
            ],
            initialValue: this.state.targetData && this.state.targetData.mnemonic
        });
        let _status = this.state.targetData ? (this.state.targetData.status == 1 ? true : false) : true;
        const status = getFieldProps('status',{
            rules:[
                { required : false ,type:'boolean' , message : '请选择状态' }
            ],
            initialValue: _status
        });
        return(
            <Modal
                visible = {this.state.visible}
                title = {this.state.title}
                confirmLoading = {this.state.confirmLoading}
                onOk = {this.onOk}
                onCancel = {this.onCancel}
            >
                <Form horizontal form={this.props.form}>
                    <FormItem {...formItemLayout} label="名称">
                        <Input {...name} placeholder="请输入名称"/>
                    </FormItem>
                    <FormItem {...formItemLayout} label="编码"><InputNumber {...code} min={0} defaultValue={1} placeholder="请输入编码"/></FormItem>
                    <FormItem {...formItemLayout} label="助记符"><Input {...mnemonic} placeholder="请输入助记符"/></FormItem>
                    <FormItem {...formItemLayout} label="启用">
                        <Switch {...status} checked={status.value}/>
                    </FormItem>
                </Form>
             </Modal>
        );
    };
}
export default FormCheckType = Form.create()(FormCheckType);

